emoji: Try to load emoji data using both language and territory
authorMarco Trevisan (Treviño) <mail@3v1n0.net>
Fri, 7 May 2021 14:03:37 +0000 (16:03 +0200)
committerMarco Trevisan (Treviño) <mail@3v1n0.net>
Fri, 7 May 2021 14:10:29 +0000 (16:10 +0200)
When loading the emoji data we just try to get the data for a language
while there may be territory specializations and emojibase provides
them.

So, split the loading function and try to load the data for the fully
defined language string (i.e. `it-ch`) before loading the generic one
for the language (i.e. `it`) and eventually falling back to the generic
english.

gtk/gtkemojichooser.c

index 97738b947c7b83b0b05afdd241721d17bec62c64..86d6572c8f310498227a1d25d83c8ecff1d6e234 100644 (file)
@@ -595,25 +595,13 @@ add_emoji (GtkWidget    *box,
   gtk_flow_box_insert (GTK_FLOW_BOX (box), child, prepend ? 0 : -1);
 }
 
-GBytes *
-get_emoji_data (void)
+static GBytes *
+get_emoji_data_by_language (const char *lang)
 {
   GBytes *bytes;
-  const char *lang;
-  char q[10];
   char *path;
   GError *error = NULL;
 
-  lang = pango_language_to_string (gtk_get_default_language ());
-  if (strchr (lang, '-'))
-    {
-      int i;
-      for (i = 0; lang[i] != '-' && i < 9; i++)
-        q[i] = lang[i];
-      q[i] = '\0';
-      lang = q;
-    }
-
   path = g_strconcat ("/org/gtk/libgtk/emoji/", lang, ".data", NULL);
   bytes = g_resources_lookup_data (path, 0, &error);
   if (bytes)
@@ -666,10 +654,40 @@ get_emoji_data (void)
     }
 
   g_clear_error (&error);
-
   g_free (path);
 
-  return g_resources_lookup_data ("/org/gtk/libgtk/emoji/en.data", 0, NULL);
+  return NULL;
+}
+
+GBytes *
+get_emoji_data (void)
+{
+  GBytes *bytes;
+  const char *lang;
+
+  lang = pango_language_to_string (gtk_get_default_language ());
+  bytes = get_emoji_data_by_language (lang);
+  if (bytes)
+    return bytes;
+
+  if (strchr (lang, '-'))
+    {
+      char q[5];
+      int i;
+
+      for (i = 0; lang[i] != '-' && i < 4; i++)
+        q[i] = lang[i];
+      q[i] = '\0';
+
+      bytes = get_emoji_data_by_language (q);
+      if (bytes)
+        return bytes;
+    }
+
+  bytes = get_emoji_data_by_language ("en");
+  g_assert (bytes);
+
+  return bytes;
 }
 
 static gboolean